import { UploadFile } from "antd/lib/upload/interface";
import { contentObj } from "screens/edit/edit.slice";
import { http } from "./http";

export const GetContent = (
  data: { id?: number | string; isOpen?: boolean } = {}
): Promise<contentObj[]> => {
  return new Promise((res) => {
    http("GetContent", { data }).then(
      (data: { data: { content: contentObj[] } }) => {
        res(data.data.content);
      }
    );
  });
};

export const AddContent = (data: {
  title: string;
  author: string;
  original: string;
  tag: string;
  imgUrl: UploadFile;
  fileUrl: UploadFile;
  tip: string;
  isnote: string;
  demoUrl: string;
}) => {
  return new Promise((res) => {
    http("AddContent", { data }).then((data) => {
      res(data.data.content);
    });
  });
};

export const UpdateContent = (data: {
  id?: number;
  title: string;
  author: string;
  original: string;
  tag: string;
  imgUrl: UploadFile;
  fileUrl: UploadFile;
  tip: string;
  isnote: string;
  demoUrl: string;
}): Promise<contentObj[]> => {
  return new Promise((res) => {
    http("UpdateContent", { data }).then(
      (data: { data: { content: contentObj[] } }) => {
        res(data.data.content);
      }
    );
  });
};

export const DeleteContent = (id: number) => {
  return new Promise((res, rej) => {
    http("DeleteContent", { data: { id } })
      .then(() => {
        res(id);
      })
      .catch((error) => {
        rej(id);
      });
  });
};
